/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.widgets.Canvas;

/**
 *
 * @author Oren
 */
public class BasicRobot implements Robot , Comparable<Robot> {
 protected String name;
 private Position start,current,pre; // starting and current positions
 protected int speed,heading;
 private boolean assign = false;


 // changing Original CODE
 //private final int speedLimit=10;// 0..10 forward speed, -10..0 backward speed
 protected int speedLimit ;// 0..10 forward speed, -10..0 backward speed
 //private final int headingLimit=20;// 0..20 degrees to the right, -20..0 to the left
 protected int headingLimit ;// 0..20 degrees to the right, -20..0 to the left

 public BasicRobot(String name){
  start = new Position(0,0);
  current = new Position(0,0);
  pre = new Position(0,0);
  speed=0;
  heading=0;

  speedLimit=10;
  headingLimit=360;
  this.name = name;
 }
 public BasicRobot(){
  start = new Position(0,0);
  current = new Position(0,0);
  pre = new Position(0,0);
  speed=0;
  heading=0;

  speedLimit=10;
  headingLimit=360;
 }

 public void setName (String name)
 {
     this.name = name;
 }

 public String getName() {return name;}


 public void setStartingPosition(Position p) {
  start.x=p.x;
  start.y=p.y;
 // it this two lines change the way things work in my code ? 
  current.x = start.x;
  current.y = start.y;
  
 }

 // enter the next move values and check that the speed and heading limit are
 // not exceded
 public void move(int speed, int heading) {
     if (speed < 0)
     {
         this.speed=Math.max(speed,(this.speedLimit*-1));
     }
     if (speed >= 0)
     {
         this.speed=Math.min(speed,this.speedLimit);
     }

     int temp = heading - this.heading ;
     temp = temp%360;
     if (temp > 0)
         this.heading = this.heading + Math.min(temp, this.headingLimit);
     if (temp <=0)
         this.heading = this.heading + Math.max(temp,(this.headingLimit*-1));
 }
 public void stop() {
  speed=0;
 }
 public Position getCurrentPosition() {
  return current;
 }
 public Position getStartPosition() {
  return start;
 }

 public double fixHeading(int hd)
 {
     return (hd*(Math.PI/180));
 }

 public void act()
 {
     double alpah = this.fixHeading(heading);

     // changed sin and cos to fix as in appendix

     this.current.x = (int) (Math.round(this.speed * Math.sin(alpah)) + start.x);
     this.current.y = (int) (Math.round(this.speed * Math.cos(alpah)) + start.y);
      start.x= current.x;
      start.y= current.y;
 }
  public void setPre(Position temp)
  {
      pre.x = temp.x;
      pre.y = temp.y;
  }

    public int getDistance() {
        int _x = (pre.x - current.x)*(pre.x - current.x);
        int _y = (pre.y - current.y)*(pre.y - current.y);
        return (int) Math.sqrt(_x+_y);

    }
    final public int compareTo(Robot arg0) {
        return name.compareTo(arg0.getName());
    }
    public int getSpeedLimit(){return speedLimit;}
    public int getHeadingLimit(){return headingLimit;}
    public int getHeading() {return heading;};
// the basic robot has no arm so return false
    public boolean pickupBox(Box b) {
       return false;
    }
// the basic robot has no arm so return false
    public boolean putdownBox() {
         return false;
    }
    public String getType()
    {
        return "";

    }
  public void setAssign()
    {
        assign = true;
    }

public boolean getAssign()
  {
      return assign;
  }




public void paint(PaintEvent e){
	Canvas c=(Canvas)e.widget;	// get the canvas
	int maxX = c.getSize().x;	// max size
	int maxY = c.getSize().y;
	int mx=maxX/2,my=maxY/2;	// mid point as (0,0)
	int myX=mx+current.x;	// set the current position
	int myY=my-current.y;	// set the current position

        // drawing the robot oval borders
	e.gc.drawOval(myX-10, myY-10, 20, 20);	// the circle

	/*
	 * draw the heading indicator here
	 * use e.gc.drawLine
	 */
        int flag = 1;
        if (speed < 0)
            flag = -1;

        double alpah = this.fixHeading(heading);
        // the X axis is going up from left pos to right
        int myX_X = (int) (Math.round(30*flag* Math.sin(alpah)) + myX);
        // the Y axis going up from up to buttom ?
        int myY_Y = myY - (int) (Math.round(30* flag * Math.cos(alpah)));

        e.gc.drawLine(myX, myY, myX_X, myY_Y);


	// the info
	e.gc.drawString(name,myX+13, myY-13,true);
	e.gc.drawString("("+current.x+","+current.y+")",myX+13, myY+13,true);
}

public void setType(String type)
{
    return;
}

}
